using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DataPaper
{
    public partial class Win : Form
    {
        public Win()
        {
            InitializeComponent();
            //this.pictureBoxTopLeft.MouseWheel += pictureBoxTopLeft_MouseWheel;
            //this.splitContainerLeft.MouseWheel += pictureBoxTopLeft_MouseWheel;
        }
        private double GetZoom()
        {
            if (trackZoom.Value > 0)
                return trackZoom.Value;
            if (trackZoom.Value == 0)
                return 1;
            else return -1.0 / trackZoom.Value;
        }
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult res = openFileScan.ShowDialog();
            if (res == DialogResult.OK)
            {
                Zoom.Cur = new Zoom(openFileScan.FileName);
                trackZoom.Value = 1;
                Zoom.Cur.Print(pictureBoxTopLeft,
                    Zoom.Cur.init_top_left.r,
                Zoom.Cur.init_top_left.c, GetZoom());

            }

        }

        //private void pictureBoxTopLeft_MouseWheel(object sender, MouseEventArgs e)
        //{
        //    if (e.Delta > 0)
        //        Zoom.ChangeZoom(true);
        //    else
        //        if (e.Delta < 0)
        //        Zoom.ChangeZoom(false);
        //    Zoom.Cur.Print(pictureBoxTopLeft, 0, 0, GetZoom()));
        //}

        private void pictureBoxTopLeft_MouseClick(object sender, MouseEventArgs e)
        {
            double z = GetZoom();
            if (e.Button == MouseButtons.Left)
            {
                Zoom.Cur.init_top_left =
                    Zoom.Cur.MouseToPoint(e.Y, e.X, pictureBoxTopLeft, z);
                trackZoom.Value = Math.Min(trackZoom.Maximum, trackZoom.Value + 2);
                labelLoc.Text = Zoom.Cur.init_top_left.ToString();
                Zoom.Cur.Print(pictureBoxTopLeft,
                    Zoom.Cur.init_top_left.r,
                Zoom.Cur.init_top_left.c, GetZoom());
            }
            if (e.Button == MouseButtons.Right)
            {
                Zoom.Cur.init_u =
                    Zoom.Cur.MouseToPoint(e.Y, e.X, pictureBoxTopLeft, z).c
                - Zoom.Cur.init_top_left.c;
                //(int)(e.X / z) - Zoom.Cur.init_top_left.c;
                labelLoc2.Text = Zoom.Cur.init_u.ToString();
            }
            if (e.Button == MouseButtons.Middle)
            {
                //double black, white;
                ////Zoom.Cur.CalibrateBW(Zoom.Cur.topLeft, out white, out black);
                List<byte> lst = new List<byte>();
                if (richTextBoxLog.Lines.Length > 20)
                    richTextBoxLog.Clear();
                List<SmallError> smallError;
                List<LargeError> largeErrors;

                int error = DoIt.Blocks(Zoom.Cur.bmp,
                    Zoom.Cur.init_top_left,
                    Zoom.Cur.init_u, 
                    //17, 12, 
                    21,15,
                    
                    out lst, out smallError, out largeErrors);
                Zoom.Cur.AddSmallErrors(smallError);
                Zoom.Cur.AddLargeErrors(largeErrors);
                richTextBoxLog.AppendText("error=" + error + Environment.NewLine);
                richTextBoxLog.AppendText("lst=" + lst.Count + Environment.NewLine);
                UInt16 infoNumPage;
                string everythingOK = DoIt.Extract(lst, out infoNumPage);

                richTextBoxLog.AppendText(everythingOK+ Environment.NewLine+
                                        "Page num=" + (infoNumPage+1) + Environment.NewLine);
                string msg = (error == 0 ? "Sucess!" : "Number of error:" + error) 
                    + Environment.NewLine
                     + everythingOK + Environment.NewLine
                        + "Page:" + (infoNumPage+1) + Environment.NewLine
                    + "Add/Replace this page?";
                if (MessageBox.Show(msg, "Add page", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    Pages.Add(infoNumPage, lst);
                    buttonWrite.Text = "Write" + Environment.NewLine + Pages.Msg();
                }
            }
            //richTextBoxLog.AppendText("Number of errori=" + i + Environment.NewLine);



            //System.Windows.Forms.Control.ModifierKeys & Keys.Shift

            //if (e.Button == MouseButtons.Left)
            //    Zoom.ChangeZoom(true);
            //if (e.Button == MouseButtons.Right)
            //    Zoom.ChangeZoom(false);
            // Zoom.Cur.Print(pictureBoxTopLeft);
        }

        private void pictureBoxTopLeft_MouseMove(object sender, MouseEventArgs e)
        {
            //Point p = pictureBoxTopLeft.PointToClient(new Point(e.X, e.Y));

            //richTextBoxLog.AppendText(p.ToString());

        }


        private void trackZoom_ValueChanged_1(object sender, EventArgs e)
        {
            Zoom.Cur.Print(pictureBoxTopLeft, Zoom.Cur.init_top_left.r,
                Zoom.Cur.init_top_left.c, GetZoom());
        }

        private void buttonWrite_Click(object sender, EventArgs e)
        {
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                Pages.Write(saveFileDialog.FileName);
            }
        }

        private void pictureBoxTopLeft_DoubleClick(object sender, EventArgs e)
        {
            trackZoom.Value =
                Math.Max(trackZoom.Minimum, trackZoom.Value - 4);
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                Zoom.Cur.bmp.Save(saveFileDialog.FileName);
            }
        }



    }
}